//  Copyright Druid Mechanics

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "GameplayTagContainer.h"
#include "UI/WidgetController/AuraWidgetController.h"
#include "OverlayWidgetController.generated.h"
struct FAuraAbilityInfo;
/**
 * FOnAttributeChangeData: 当属性值改变时的短暂的参数结构体。
 */
struct FOnAttributeChangeData;
class UAuraUserWidget;
class UTexture2D;
class UDataTable;
class UAbilityInfo;
class UAuraAbilitySystemComponent;
/**
 * DataTable: 主要用于存放静态数据，类似于Excel表格，常用于简单数据存储，可用于存储格式固定的静态数据。
 * DataAsset: 主要用于存放动态数据，修改便捷，数据格式灵活，可直接使用数据引用，可用于存储格式较为复杂且经常改动的数据，比DataTable更加灵活。
 */
USTRUCT(BlueprintType)
struct FUIWidgetRow : public FTableRowBase
{
	GENERATED_BODY()
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	FGameplayTag MessageTag{FGameplayTag()};
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	FText Message{FText()};
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	TSubclassOf<UAuraUserWidget> MessageWidget;
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UTexture2D* Image{nullptr};
};


/**
 * DECLARE_DYNAMIC_MULTICAST_DELEGATE的事件命名格式FOn+EName+Signature EName是事件的名字。
 */

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnAttributeChangedSignature, float, NewValue);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnLevelChangedSignature, int32, NewLevel, bool, bLevelUp);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnMessageWidgetRowSignature, FUIWidgetRow, Row);


/**
 * Blueprintable； 将此类公开为用于创建蓝图的可接受基类。
 * BlueprintType： 将此类公开为可用于蓝图中的变量的类型。
 * LINK: https://dev.epicgames.com/documentation/zh-cn/unreal-engine/class-specifiers
 */
UCLASS(BlueprintType, Blueprintable)
class AURA_API UOverlayWidgetController : public UAuraWidgetController
{
	GENERATED_BODY()
public:
	virtual void BroadcastInitialValue() override;
	virtual void BindCallbacksToDependencies() override;

	UPROPERTY(BlueprintAssignable, Category = "GAS|Attribgutes")
	FOnAttributeChangedSignature OnHealthChanged;
	UPROPERTY(BlueprintAssignable, Category = "GAS|Attribgutes")
	FOnAttributeChangedSignature OnMaxHealthChanged;
	UPROPERTY(BlueprintAssignable, Category = "GAS|Attributes")
	FOnAttributeChangedSignature OnManaChanged;
	UPROPERTY(BlueprintAssignable, Category = "GAS|Attributes")
	FOnAttributeChangedSignature OnMaxManaChanged;
	UPROPERTY(BlueprintAssignable, Category = "GAS|Messages")
	FOnMessageWidgetRowSignature MessageWidgetRowDelegate;
	UPROPERTY(BlueprintAssignable, Category = "GAS|XP")
	FOnAttributeChangedSignature OnXPPercentChangedDelegate;
	UPROPERTY(BlueprintAssignable, Category = "GAS|Level")
	FOnLevelChangedSignature OnPlayerLevelChangedDelegate;
	
protected:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Widget Data")
	TObjectPtr<UDataTable> MessageWidgetDataTable;
	
	template <typename T>
	T* GetDataTableRowByTag(UDataTable* DataTable, const FGameplayTag& Tag);

	UFUNCTION()
	void OnXPChanged(int32 NewXP);

	void OnAbilityEquipped(const FGameplayTag& AbilityTag, const FGameplayTag& Status, const FGameplayTag& Slot, const FGameplayTag& PreviousSlot);
};

template <typename T>
T* UOverlayWidgetController::GetDataTableRowByTag(UDataTable* DataTable, const FGameplayTag& Tag)
{
	/**
	 * FindRow: 函数用于查找给定名称的表行。
	 */
	return DataTable->FindRow<T>(Tag.GetTagName(), TEXT(""));
}
